
package com.yingwei.mining.model.test;

import java.util.List;

import org.junit.Assert;
import org.junit.Test;

import com.yingwei.mining.model.businessobject.${pojo.getDeclarationName()};
import com.yingwei.mining.model.service.${pojo.getDeclarationName()}Service;
import com.yingwei.mining.model.service.impl.${pojo.getDeclarationName()}ServiceImpl;
import com.yingwei.mining.model.util.HibernateUtil;

public class Test${pojo.getDeclarationName()}Service
{
	public static void main(String[] args)
	{
		new Test${pojo.getDeclarationName()}Service().testService();
	}
	@Test
	public void testService()
	{
		try
		{
			${pojo.getDeclarationName()}Service service = new ${pojo.getDeclarationName()}ServiceImpl();
		    HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
		    
		    // get all records first and remember the total counts
		    List<${pojo.getDeclarationName()}> lst = service.get${pojo.getDeclarationName()}s();
		    int total = lst.size();
			System.out.println("total:"+total);

			// create 2 values
			${pojo.getDeclarationName()} val1 = new ${pojo.getDeclarationName()}();
			${pojo.getDeclarationName()} val2 = new ${pojo.getDeclarationName()}();

<#foreach field in pojo.getAllPropertiesIterator()>
<#if pojo.getJavaTypeName(field, jdk5).equals("String")>
			val1.set${pojo.getPropertyName(field)}("test 1: ${field.name}");
</#if>
</#foreach>

<#foreach field in pojo.getAllPropertiesIterator()>
<#if pojo.getJavaTypeName(field, jdk5).equals("String")>
			val2.set${pojo.getPropertyName(field)}("test 2: ${field.name}");
</#if>
</#foreach>

		    service.create${pojo.getDeclarationName()}(val1);
<#if !pojo.generateAnnIdGenerator().equals("") >
		    Long id1 = val1.getId();
		    System.out.println("id1:" + id1);
</#if>

		    service.create${pojo.getDeclarationName()}(val2);
<#if !pojo.generateAnnIdGenerator().equals("") >
		    Long id2 = val2.getId();
		    System.out.println("id2:" + id2);
</#if>

<#if !pojo.generateAnnIdGenerator().equals("") >
			// read
		    ${pojo.getDeclarationName()} readBack${pojo.getDeclarationName()} = service.read${pojo.getDeclarationName()}(new Long(id1));
<#foreach field in pojo.getAllPropertiesIterator()>
<#if pojo.getJavaTypeName(field, jdk5).equals("String")>
		    System.out.println("field ${pojo.getPropertyName(field)}:" + readBack${pojo.getDeclarationName()}.get${pojo.getPropertyName(field)}());
</#if>
</#foreach>
		    Assert.assertEquals(readBack${pojo.getDeclarationName()}, val1);
		    Assert.assertNotSame("not same:",readBack${pojo.getDeclarationName()}, val2);
		    Assert.assertNotSame("not same:",val1, val2);
</#if>

		    // update
<#foreach field in pojo.getAllPropertiesIterator()>
<#if pojo.getJavaTypeName(field, jdk5).equals("String")>
		    val2.set${pojo.getPropertyName(field)}("update");
</#if>
</#foreach>	
		    service.update${pojo.getDeclarationName()}(val2);

		    // get all records and compare the total counts
		    List<${pojo.getDeclarationName()}> lst2 = service.get${pojo.getDeclarationName()}s();
		    int total2 = lst2.size();
			System.out.println("total after create:"+total2);
			Assert.assertEquals(total+2, total2);
	
		    // delete
		    service.delete${pojo.getDeclarationName()}(val1);
		    service.delete${pojo.getDeclarationName()}(val2);
		    
		    for (${pojo.getDeclarationName()} value : lst2) {
<#foreach field in pojo.getAllPropertiesIterator()>
<#if pojo.getJavaTypeName(field, jdk5).equals("String")>
			    System.out.println("field ${pojo.getPropertyName(field)}:" + value.get${pojo.getPropertyName(field)}());
</#if>
</#foreach>
			} 
		    //HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
		    HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
		}
		catch(Exception e )
		{
		    HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().rollback();
			e.printStackTrace();
			Assert.fail("failed" + e.getMessage());
		}
	}
}
